package nl.borchef.map_2d_game;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;

public class tank {
	private game mo_game = null;
	private int mi_max_speed = 3;
	private int mi_speed = 2;
	private int mi_middle_position_x = 0;
	private int mi_middle_position_y = 0;
	private int mi_offset_position_x = 0;
	private int mi_offset_position_y = 0;

	private int mi_size_x = 100;
	private int mi_size_y = 100;
	
	private int mi_canon_direction = 0;
	private int mi_canon_direction_change = 0;
	private int mi_canon_size_x = 18;
	private int mi_canon_size_y = 65;
	private int mi_dome_outer_size_x = 60;
	private int mi_dome_outer_size_y = 60;
	private int mi_dome_inner_size_x = 40;
	private int mi_dome_inner_size_y = 40;
	private int mi_dome_hatch_size_x = 12;
	private int mi_dome_hatch_size_y = 10;
	private int mi_hatch_offset_x = -6;
	private int mi_hatch_offset_y = -5;

	private boolean mi_driving = false;
	private int mi_direction = 0;
	private int mi_turn_speed_x = 0;
	private int mi_turn_speed_y = 0;
	private int mi_max_turn_speed = 5;

	private int mi_body_stage = 0;
	private int mi_body_size_x = 60;
	private int mi_body_size_y = 70;

	private int mi_track_stage = 0;
	private int mi_track_offset = 20;
	private int mi_track_size_x = 25;
	private int mi_track_size_y = 80;
	
	private paint_colors mo_paint_colors;
	private GradientDrawable mo_dome_outer_drawable;
	private GradientDrawable mo_dome_inner_drawable;
	private GradientDrawable mo_canon_drawable;
	private GradientDrawable mo_body_drawable;
	private Paint mo_track_paint_1;
	private Paint mo_track_paint_2;
	public tank(paint_colors paint_colors, game game ){
		mo_game = game;
		mo_paint_colors = paint_colors;

		// dome drawables
		mo_dome_outer_drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFF666666, 0xFF666666, 0xFFAAAAAA, 0xFF666666, 0xFFAAAAAA, 0xFFAAAAAA, 0xFF666666, 0xFF666666,
				0xFFAAAAAA, 0xFF666666, 0xFF666666 });
		mo_dome_outer_drawable.setShape(GradientDrawable.OVAL);
		mo_dome_outer_drawable.setGradientRadius((float)40);
		mo_dome_outer_drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		
		mo_dome_inner_drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFF666666, 0xFFAAAAAA, 0xFFBBBBBB, 0xFF666666, 0xFFBBBBBB,
                0xFFAAAAAA });
		mo_dome_inner_drawable.setShape(GradientDrawable.OVAL);
		mo_dome_inner_drawable.setGradientRadius((float)(Math.sqrt(2) * 60));
		mo_dome_inner_drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		
		// canon drawables
		mo_canon_drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFF000000, 0xFF888888, 0xFF000000, 0xFF999999, 0xFF888888,
				0xFF000000 });
		mo_canon_drawable.setShape(GradientDrawable.RECTANGLE);
		mo_canon_drawable.setGradientRadius((float)mi_canon_size_x / 2);
		mo_canon_drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);

		// body drawables
		mo_body_drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] { 0xFF666666, 0xFFAAAAAA, 0xFFBBBBBB, 0xFF666666, 0xFFBBBBBB,
				0xFF666666 });
		mo_body_drawable.setShape(GradientDrawable.RECTANGLE);
		mo_body_drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		mo_body_drawable.setCornerRadii(new float[] { 4, 7, 4, 7, 4, 7, 4, 7 });

        // track drawables
		Bitmap bm = Bitmap.createBitmap(mi_track_size_x, 8, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bm);
        c.drawColor(Color.RED);
        Paint p = new Paint();
        p.setColor(Color.BLACK);
        c.drawRect(0, 0, mi_track_size_x, 4, p);
        BitmapShader track_shader = new BitmapShader(bm, Shader.TileMode.REPEAT,
                Shader.TileMode.REPEAT);
        mo_track_paint_1 = new Paint(Paint.FILTER_BITMAP_FLAG);
        mo_track_paint_1.setShader(track_shader);
		bm = Bitmap.createBitmap(mi_track_size_x, 8, Bitmap.Config.RGB_565);
        c = new Canvas(bm);
        c.drawColor(Color.RED);
        p = new Paint();
        p.setColor(Color.BLACK);
        c.drawRect(0, 4, mi_track_size_x, 8, p);
        track_shader = new BitmapShader(bm, Shader.TileMode.REPEAT,
                Shader.TileMode.REPEAT);
        mo_track_paint_2 = new Paint(Paint.FILTER_BITMAP_FLAG);
        mo_track_paint_2.setShader(track_shader);
	
	}
	public void set_canvas_width(int width){
		mi_middle_position_x = (width /2);
	}
	public void set_canvas_height(int height){
		mi_middle_position_y = (height /2);
	}
	public void stop(){
		mi_driving = false;
		mi_speed = 0;
		mo_game.play_tank_driving_sound(false);
	}
	public void turn(int direction){
        if(direction < 0 ) direction += 180;
		if( mi_direction > direction && direction != 0)
		{
        	mi_turn_speed_y = 0;
			if( mi_turn_speed_x < mi_max_turn_speed )
				mi_turn_speed_x++;
			mi_direction -= mi_turn_speed_x;
			if( mi_turn_speed_x > 2 ){
				mi_driving = false;
				mo_game.play_tank_driving_sound(false);
			}

		}
		else if( mi_direction < direction  && direction != 0 )
		{
			mi_turn_speed_x = 0;
			if( mi_turn_speed_y < mi_max_turn_speed )
				mi_turn_speed_y++;
			mi_direction += mi_turn_speed_y;
			if( mi_turn_speed_y > 2 ){
				mi_driving = false;
				mo_game.play_tank_driving_sound(false);
			}
		}
		else
		{
			mi_turn_speed_y = 0;
			mi_turn_speed_x = 0;
			mi_driving = true;
			mo_game.play_tank_driving_sound(true);
		}
    }
	public void draw(Canvas canvas){
    	
		if( (int)(Math.random() * 5) == 1 ){
			mi_canon_direction_change = (int)(Math.random() * 3);
		}
		if( mi_canon_direction_change == 1 ){
			mi_canon_direction += 2;
		}
		if( mi_canon_direction_change == 2 ){
			mi_canon_direction -= 2;
		}
		mi_canon_direction %= 360;
		
		Rect dome_hatch_rect = new Rect(	( mi_middle_position_x - (mi_dome_hatch_size_x / 2) ) + mi_hatch_offset_x,
											( mi_middle_position_y - (mi_dome_hatch_size_y / 2) ) + mi_hatch_offset_y,
											( mi_middle_position_x  + (mi_dome_hatch_size_x / 2) ) + mi_hatch_offset_x,
											( mi_middle_position_y  + (mi_dome_hatch_size_y / 2) ) + mi_hatch_offset_y);
		Rect dome_inner_rect = new Rect(	mi_middle_position_x - (mi_dome_inner_size_x / 2),
   											mi_middle_position_y - (mi_dome_inner_size_y / 2),
   											mi_middle_position_x  + (mi_dome_inner_size_x / 2),
   											mi_middle_position_y  + (mi_dome_inner_size_y / 2));
		Rect dome_outer_rect = new Rect(	mi_middle_position_x - (mi_dome_outer_size_x / 2),
   											mi_middle_position_y - (mi_dome_outer_size_y / 2),
   											mi_middle_position_x  + (mi_dome_outer_size_x / 2),
   											mi_middle_position_y  + (mi_dome_outer_size_y / 2));
    	Rect canon_rect = new Rect( 		mi_middle_position_x - (mi_canon_size_x / 2),
											mi_middle_position_y,
											mi_middle_position_x  + (mi_canon_size_x / 2),
											mi_middle_position_y  + mi_canon_size_y );
    	Rect body_rect = new Rect(			mi_middle_position_x - ( mi_body_size_x / 2 ),
											mi_middle_position_y - ( mi_body_size_y / 2 ),
											mi_middle_position_x  + (mi_body_size_x / 2),
											mi_middle_position_y  + ( mi_body_size_y / 2 ) );
    	Rect track_left_rect = new Rect((	mi_middle_position_x - mi_track_offset) - ( mi_track_size_x / 2 )  ,
										 	mi_middle_position_y - ( mi_track_size_y / 2 ),
										 	(mi_middle_position_x  -  mi_track_offset ) + (mi_track_size_x / 2) ,
										 	mi_middle_position_y  + ( mi_track_size_y / 2 ) );
    	Rect track_right_rect = new Rect((	mi_middle_position_x + mi_track_offset) - ( mi_track_size_x / 2 )  ,
    										mi_middle_position_y - ( mi_track_size_y / 2 ),
    										(mi_middle_position_x  +  mi_track_offset ) + (mi_track_size_x / 2) ,
    										mi_middle_position_y  + ( mi_track_size_y / 2 ) );
    	
    	//draw tracks
        canvas.save();
        canvas.rotate((float) mi_direction + 90, (float) mi_middle_position_x, (float)mi_middle_position_y);
        if( mi_track_stage == 0 ){
            canvas.drawRect(track_left_rect, mo_track_paint_1);
            canvas.drawRect(track_right_rect, mo_track_paint_1);
            if( mi_driving )
            	mi_track_stage = 1;
        }
        else{
            canvas.drawRect(track_left_rect, mo_track_paint_2);
            canvas.drawRect(track_right_rect, mo_track_paint_2);
            if( mi_driving )
            	mi_track_stage = 0;
        }
        canvas.restore();
    	
    	//draw body
        canvas.save();
        canvas.rotate((float) mi_direction + 90, (float) mi_middle_position_x, (float)mi_middle_position_y);
//        if( mi_body_stage == 0 ){
    		mo_body_drawable.setBounds(body_rect);
    		mo_body_drawable.draw(canvas);
//        }
        canvas.restore();

        mo_canon_drawable.setBounds(canon_rect);
        canvas.save();
        canvas.rotate((float) mi_canon_direction, (float) mi_middle_position_x, (float)mi_middle_position_y);
        mo_canon_drawable.draw(canvas);
        canvas.restore();
		
    	canvas.save();
        canvas.rotate((float) mi_canon_direction - 90, (float) mi_middle_position_x, (float)mi_middle_position_y);
		mo_dome_outer_drawable.setBounds(dome_outer_rect);
    	mo_dome_outer_drawable.draw(canvas);
        canvas.restore();
 
    	canvas.save();
        canvas.rotate((float) mi_canon_direction - 90, (float) mi_middle_position_x, (float)mi_middle_position_y);
    	mo_dome_inner_drawable.setBounds(dome_inner_rect);
    	mo_dome_inner_drawable.draw(canvas);
 //   	mo_body_drawable.setBounds(dome_hatch_rect);
 //   	mo_body_drawable.draw(canvas);
    	Paint p = mo_paint_colors.get_line_paint_black();
    	p.setStyle(Style.FILL_AND_STROKE);
    	p.setARGB(255, 240, 240, 240);
    	canvas.drawRect(dome_hatch_rect, p);
        canvas.restore();
    	//canvas.drawLine(mi_middle_position_x, 0, mi_middle_position_x, 1000, mo_paint_colors.get_paint_red());
    	//canvas.drawLine(0, mi_middle_position_y, 1000, mi_middle_position_y, mo_paint_colors.get_paint_red());
	}	
	public int get_speed() {
		if( mi_driving )
		{
			mi_driving = false;
			mo_game.play_tank_driving_sound(false);
			if( mi_speed < mi_max_speed )
				mi_speed++;
			return mi_speed;
		}
		mo_game.play_tank_driving_sound(true);
		return 0;
	}
}
